package pure.core
{
	import com.adobe.images.PNGEncoder;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	
	import pure.DesB;
	import pure.events.MapEvent;
	import pure.manager.MapManager;
	
public class Map
{
	public function Map()
	{
	}
	
	// Tile大小
	public var tileSize:Number;
	
	// tile数目(X轴×Y轴)
	public var numTile:Point;
	
	public var deepRenderData:Array;
	
	public var shallowRenderData:Array;
	
	public var landformData:Array;
	
	// Sheet中的每行固定Tile数目(最大32)
	public var numTileInsheet:int;
	public var rowInSheet:int;
	
	protected var _sheet:BitmapData;

	
	
	// [存]深层渲染数据
	public function setDeepRenderData(tileX:int,tileY:int,value:int):void{
		deepRenderData[tileY][tileX]=value;
	}
	
	// [存]浅层渲染数据
	public function setShallowRenderData(tileX:int,tileY:int,value:int):void{
		shallowRenderData[tileY][tileX]=value;
	}
	
	
	// [存]地形数据
	public function setLandformData(tileX:int,tileY:int,value:int):void{
		landformData[tileY][tileX]=value;
	}
	
	// 替换Sheet
	public function setSheet(image:BitmapData):void{
		if(_checkSheetSize(image)){
			_sheet=new BitmapData(numTileInsheet*tileSize,image.height);
			_sheet.copyPixels(image,new Rectangle(0,0,image.width,image.height),new Point(0,0));
			rowInSheet=_sheet.height/tileSize;
		}
	}
		
	// 加入Sheet(扩大)
	public function addSheet(image:BitmapData):void{
		if(_checkSheetSize(image)){
			if(_sheet){
				var w:Number=tileSize*numTileInsheet;
				var h:Number=image.height+_sheet.height;
				
				var buffer:BitmapData=new BitmapData(w,h);
				var rect:Rectangle=new Rectangle(0,0,_sheet.width,_sheet.height);
				buffer.copyPixels(_sheet,rect,new Point(0,0));
				rect=new Rectangle(0,0,image.width,image.height);
				buffer.copyPixels(image,rect,new Point(0,_sheet.height));
				
				_sheet=buffer;
			}
			else{
				_sheet=new BitmapData(numTileInsheet*tileSize,image.height);
				_sheet.copyPixels(image,new Rectangle(0,0,image.width,image.height),new Point(0,0));
			}
			rowInSheet=_sheet.height/tileSize;
		}
	}
	
	// 清除Sheet
	public function clearSheet():void{
		_sheet.dispose();
		_sheet=null;
		
		// Sheet变化
		MapManager.instance.dispatchEvent(new MapEvent(MapEvent.SHEET_CHANGE));
	}

	
	
	public function get width():Number{return tileSize*numTile.x;}
	public function get height():Number{return tileSize*numTile.y;}

	public function get sheet():BitmapData{return _sheet;}

	
	public function toDeepRenderString():String{
		var str:String='DeepRenderData = \r';
		var len:int=deepRenderData.length;
		for(var i:int=0;i<len;i++){
			var temp:Array=deepRenderData[i];
			if(i==0){
				str+='[['+temp.join(',')+'],\r';
			}
			else if(i==len-1){
				str+='['+temp.join(',')+']]';
			}
			else{
				str+='['+temp.join(',')+'],\r';
			}
		}
		return str;
	}
	
	public function toShallowRenderString():String{
		var str:String='ShallowRenderData = \r';
		var len:int=shallowRenderData.length;
		for(var i:int=0;i<len;i++){
			var temp:Array=shallowRenderData[i];
			if(i==0){
				str+='[['+temp.join(',')+'],\r';
			}
			else if(i==len-1){
				str+='['+temp.join(',')+']]';
			}
			else{
				str+='['+temp.join(',')+'],\r';
			}
		}
		return str;
	}
	
	public function toLandformString():String{
		var str:String='LandformData= \r';
		var len:int=landformData.length;
		for(var i:int=0;i<len;i++){
			var temp:Array=landformData[i];
			if(i==0){
				str+='[['+temp.join(',')+'],\r';
			}
			else if(i==len-1){
				str+='['+temp.join(',')+']]';
			}
			else{
				str+='['+temp.join(',')+'],\r';
			}
		}
		return str;
	}
	
	public function encode():ByteArray{
		var bytes:ByteArray=new ByteArray();
		bytes.writeFloat(tileSize);
		bytes.writeShort(numTile.x);
		bytes.writeShort(numTile.y);
		bytes.writeShort(numTileInsheet);
		bytes.writeObject(deepRenderData);
		bytes.writeObject(shallowRenderData);
		bytes.writeObject(landformData);
		var temp:ByteArray=DesB.encodeBitmapData(MapManager.instance.currMap.sheet);
		bytes.writeBytes(temp);
		bytes.compress();
		return bytes;
	}
	
	public function toString():String{
		return "Tile大小:"+tileSize+
			"\nTile数目:"+numTile.x+"×"+numTile.y+
			"\n世界范围 :"+numTile.x*tileSize+"×"+numTile.y*tileSize;
	}
	
	// 检查图片是否符合要求
	private function _checkSheetSize(image:BitmapData):Boolean{
		if(!(image.width/tileSize is int)&&!(image.height/tileSize is int)){
			Alert.show("Sheet图片 - 宽度("+_sheet.width+")×高度("+_sheet.height+") - 均不适合","提示");
			return false;
		}
		if(!(image.width/tileSize is int)){
			Alert.show("Sheet图片 - 宽度("+image.width+") - 不适合","提示");
			return false;
		}
		if(!(image.height/tileSize is int)){
			Alert.show("Sheet图片 - 高度("+image.height+") - 不适合","提示");
			return false;
		}
		return true;
	}
}
}